// @ts-nocheck

var express = require('express');

const { rmSync } = require('fs');

var router = express.Router();

const multer = require('multer'); 

require('../db/connect');

const {FindOneData,FindManyData,InsertManyData,UpdateManyData,RemoveData} = require('../db/index');

const {getToken,checkToken} = require('../utils/token');

const {user_model,role_model,type_model,cate_model,tran_model,good_model,msg_model} = require('../db/model');


// 测试接口
router.get('/test',(req,res)=>{
    res.json({
        code:200,
        msg:"请求成功！",
        data:""
    });
});


// 用户注册
router.post('/register',(req,res)=>{
    let body = req.body ; 
    body.time = new Date() ; 
    FindOneData({
        model:user_model,
        query:{
            $or:[
                {
                    username:body.username
                },
                {
                    phone:body.phone
                }
            ]
        },
        res,
        callback(result){
            if(result){
                res.json({
                    code:401,
                    msg:'用户已存在，请重新注册',
                    data:result,
                });
            } else {
                InsertManyData({
                    model:user_model,
                    data:body,
                    res,
                    msg:'注册成功！'
                });
            }
        }
    });
});


// 用户名登录
router.post('/login',(req,res)=>{
    let body = req.body ; 
    FindOneData({
        model:user_model,
        query:body,
        res,
        callback(result){
            if(result){
                let token = getToken(body);
                res.json({
                    code:200,
                    msg:'登录成功！',
                    data:result,
                    token
                });
            } else {
                res.json({
                    code:401,
                    msg:'登录失败，用户名或密码错误!',
                    data:result
                });
            }
        }
    });
});


// 手机号登录
router.post('/phone',(req,res)=>{
    let body = req.body ; 
    FindOneData({
        model:user_model,
        query:{phone},
        res,
        callback:(result)=>{
            if(result){
                let token = getToken(result.username);
                res.json({
                    code:200,
                    msg:'登录成功！',
                    data:result,
                    token
                });
            } else {
                res.json({
                    code:401,
                    msg:'用户不存在,请注册',
                    data:result,
                });
            }
        }
    });
});

// 获取用户信息
router.post("/getUserInfo",(req,res)=>{
    checkToken(req,res,({username})=>{
        FindOneData({
            model:user_model,
            query:{username},
            msg:'获取个人信息成功！',
            res
        });
    });
});


// 获取所有用户的接口
router.post("/getUserList",(req,res)=>{
    checkToken(req,res,({username})=>{
        FindManyData({
            model:user_model,
            msg:'获取个人信息成功！',
            res
        });
    });
});


// 用户修改信息
router.post('/changeUser',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        UpdateManyData({
            model:user_model,
            query:{username},
            data:body,
            res,
            msg:'成功！'
        });
    });
});

// 删除用户
router.post('/removeUser',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        RemoveData({
            model:user_model,
            query:body,
            res,
            msg:'用户删除成功！',
            callback:result=>{
                if(result){
                    res.json({
                        code:200,
                        msg:'用户删除成功！',
                        data:result,
                    });
                } else {
                    res.json({
                        code:401,
                        msg:'用户删除失败！',
                        data:result,
                    });
                }
            }
        });
    });
});

// 添加类别
router.post('/addCate',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:cate_model,
            data:body,
            res,
            msg:'添加成功！'
        });
    });
});
// 修改类别
router.post('/updCate',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        UpdateManyData({
            model:cate_model,
            query:{_id:body._id},
            data:body,
            res,
            msg:'修改成功！'
        });
    });
});
// 获取类别
router.post('/getCate',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        FindManyData({
            model:cate_model,
            res,
            msg:'获取数据成功!'
        });
    });
});

// 添加类型
router.post('/addType',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:type_model,
            data:body,
            res,
            msg:'添加成功！'
        });
    });
});
// 修改类型
router.post('/updType',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        UpdateManyData({
            model:type_model,
            query:{_id:body._id},
            data:body,
            res,
            msg:'修改成功！'
        });
    });
});
// 获取类型
router.post('/getType',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        FindManyData({
            model:type_model,
            res,
            msg:'获取信息完成！'
        });
    });
});
// 添加权限角色
router.post('/addRole',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:role_model,
            data:body,
            res,
            msg:'添加成功！'
        });
    });
});

// 修改权限角色
router.post('/updRole',(req,res)=>{
    let body = req.body ;
    checkToken(req,res,({username})=>{
        UpdateManyData({
            model:role_model,
            query:{_id:body._id},
            data:body,
            res,
            msg:'修改成功！'
        });
    }); 
});

// 获取权限角色
router.post('/getRole',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        FindManyData({
            model:role_model,
            res,
            msg:'获取数据成功！'
        });
    });
});
// 添加轮播
router.post('/addTran',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:tran_model,
            data:body,
            res,
            msg:'添加成功！'
        });
    });
});
// 修改轮播
router.post('/updTran',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        UpdateManyData({
            model:tran_model,
            query:{_id:body._id},
            data:body,
            res,
            msg:'修改成功！'
        });
    });
});
// 获取轮播
router.post('/getTran',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        FindManyData({
            model:tran_model,
            res,
            msg:'获取数据成功！'
        });
    });
});

// 删除轮播
router.post('/delTran',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        RemoveData({
            model:tran_model,
            query:body,
            res,
            msg:'删除成功！'
        });
    });
});
// 添加商品
router.post('/addGood',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:good_model,
            data:body,
            res,
            msg:'添加成功！'
        });
    });
});
// 修改商品
router.post('/updGood',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        UpdateManyData({
            model:good_model,
            query:{_id:body._id},
            data:body,
            res,
            msg:'修改成功！'
        });
    });
});
// 获取商品
router.post('/getGood',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        FindManyData({
            model:good_model,
            query:body,
            res,
            msg:'获取数据成功！'
        });
    });
});
// 删除商品
router.post('/delGood',(req,res)=>{
    let body = req.body ; 
    checkToken(req,res,({username})=>{
        RemoveData({
            model:good_model,
            query:body,
            res,
            msg:'删除成功！'
        });
    });
});


// 定义文件上传的目录
const storage = multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,'./public/images/uploads');
    },
    filename:function(req,file,cb){
        const uniqueSuffix = 'manage_'+Date.now()+'_'+Math.round(Math.random() * 1E9) ; 
        cb(null,file.fieldname+'_'+uniqueSuffix+file.originalname);
    }
});

const upload = multer({storage:storage}).any();
// 文件上传接口
router.post('/uploadfiles',upload,(req,res)=>{
    let path = req.files[0].path ; 
    res.json({
        code:200,
        msg:'上传成功！',
        path,
        data:path
    });
});


router.get('/getUsers',(req,res)=>{
    let body = req.query ; 
    FindManyData({
        model:msg_model,
        query:body,
        res,
        msg:'数据查询成功！'
    });
});

router.post('/postUsers',(req,res)=>{
    let body = req.body ; 
    body.time = new Date();
    InsertManyData({
        model:msg_model,
        data:body,
        res,
        msg:'数据添加成功！'
    });
});

router.post('/updUsers',(req,res)=>{
    let body = req.body ; 
    body.time = new Date();
    // @ts-ignore
    UpdateManyData({
        model:msg_model,
        query:{_id:body._id},
        data:body,
        res,
        msg:'数据修改成功！'
    });
});

router.post('/delUsers',(req,res)=>{
    let body = req.body ; 
    body.time = new Date();
    RemoveData({
        model:msg_model,
        query:{_id:body._id},
        res,
        msg:'数据删除成功！'
    });
});

router.get('/getGoodsList',(req,res)=>{
    FindManyData({
        model:cate_model,
        query:{},
        res,
        msg:"数据获取成功！"
    });
});

module.exports = router;
